from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import redirect
from django.urls import reverse
from myadmin.models import Shop, Category, Product, Orders, Payment, OrderDetail
# Create your views here.


def index(request):
    # 个人中心首页
    return render(request, "mobile/member.html")


def orders(request):
    # 个人中心浏览订单
    # 浏览订单
    mod = Orders.objects
    # 获取当前店铺id号
    mid = request.session['mobileuser']['id']  # 获取当前会员id号
    olist = mod.filter(member_id=mid)
    # 获取、判断并封装状态status搜索条件
    status = request.GET.get('status', '')
    if status != '':
        olist = olist.filter(status=status)
    list2 = olist.order_by('-id')  # 对id降序排序

    # 遍历当前订单，封装订单详情信息
    orders_status = ['无', '排队中', '已撤销', '已完成']
    for vo in list2:
        plist = OrderDetail.objects.filter(order_id=vo.id)
        vo.plist = plist
        num = 0
        for vi in plist:
            num = num+vi.quantity
        vo.num = num
        vo.statusinfo = orders_status[vo.status]
    context = {"orderslist": list2}
    return render(request, "mobile/member_orders.html", context)


def detail(request):
    # 个人中心中的订单详情
    pid = request.GET.get('pid', 0)
    order = Orders.objects.get(id=pid)
    plist = OrderDetail.objects.filter(order_id=order.id)
    order.plist = plist
    num = 0
    for vi in plist:
        num = num + vi.quantity
    order.num = num
    shop = Shop.objects.only("name").get(id=order.shop_id)
    order.shopname = shop.name
    orders_status = ['无', '排队中', '已撤销', '已完成']
    order.statusinfo = orders_status[order.status]
    context = {"order": order}
    return render(request, "mobile/member_detail.html", context)


def logout(request):
    # 执行会员退出
    del request.session['mobileuser']
    return render(request, "mobile/register.html")
